package com.dz.Controller;

import com.dz.domain.goods;
import com.dz.userService.impl.goodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class goodsController {

    @Autowired
    goodsServiceImpl goodsService;

    /**
     * 查询所有商品
     * */
    @ResponseBody
    @RequestMapping("/findGoods")
    public List<goods> findGoods(@RequestBody goods goodsName){
        List<goods> goodsList = goodsService.findAllGoods(goodsName);
/*        Date date =  new Date();

        DateFormat df1 = DateFormat.getDateInstance();
        //获取时间 时分秒
        DateFormat df2 = DateFormat.getTimeInstance();
        System.out.println(df1.format(date) + "," + df2.format(date));*/
        return goodsList;
    }

    /**
     * 删除商品
     * */
    @ResponseBody
    @PostMapping("/delGoods")
    public boolean delGoods(@RequestBody int goodsId){
        int rows = goodsService.delGoodsInfo(goodsId);
        if (rows == 1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 添加商品
     * */
    @ResponseBody
    @PostMapping("/inGoodsInfo")
    public int inGoodsInfo(@RequestBody goods g){
        int rows = goodsService.insertGoods(g);
        if(rows == 1){
            return rows;
        }else{
            return 0;
        }
    }

    /**
     * 修改数据
     * */
    @ResponseBody
    @PostMapping("/upDateGoodsInfo")
    public int upDateGoodsInfo(@RequestBody goods g){
        int rows = goodsService.upDataGoodsInfo(g);
        return rows;
    }

    /**
     * 购物车查询数据
     * */
    @ResponseBody
    @GetMapping("/getGoodsCarInfo")
    public List<goods> getGoodsCarInfo(@RequestParam Integer[] ids){
        List<goods> goodsList = null;
        List<goods> goodsList2 = new ArrayList<goods>();

        goods goodsObj = new goods();
        for (Integer i : ids){
            goodsObj.setGoodsId(i);
            goodsList = goodsService.findAllGoods(goodsObj);
            for (goods goodsObj2 : goodsList){
                goodsList2.add(goodsObj2);
            }
        }
        return goodsList2;
    }
}
